package edu.umuc.cmsc495_final_project;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JTree;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;
import javax.swing.tree.DefaultMutableTreeNode;

public class barGUI extends JFrame implements allGUI, TableModelListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = -5289968412298895823L;
	JPanel orderPane;
	JPanel detailPane;
	JPanel stoolPane;

	JScrollPane tableScrollPane;
	JScrollPane menuScrollPane;

	JSplitPane leftSplitPane;
	JSplitPane splitPane;

	JButton newGuest;
	JButton transferTicket;
	JButton closeTicket;

	JLabel lblDetail;
	JLabel lblTax;
	JLabel lblTotal;

	JTextField fldReceiptTot;
	
	JTextArea txtAreaReceipt;

	JTable jtbReceipt;

	newDiner diner;

	public barGUI() {

		//
		setName("Bar");
		setBounds(0, panelStartY, panelWidth, panelEndHeight);
		setVisible(true);
		setLayout(new BorderLayout());
		removeNotify();
		setUndecorated(true);
		addNotify();

		// show main body
		mainBody();
	}

	public void mainBody() {
		jtbReceipt = new JTable(new MyTableModelRecipt());
		jtbReceipt.setVisible(true);
		jtbReceipt.getModel().addTableModelListener(this);
		jtbReceipt.getColumnModel().getColumn(0).setPreferredWidth(10);
		jtbReceipt.getColumnModel().getColumn(1).setPreferredWidth(25);
		jtbReceipt.getColumnModel().getColumn(2).setPreferredWidth(10);
		
		txtAreaReceipt = new JTextArea(); // replace to jtable.. not enough time
		
		// this will house both the table display area and the ordering tab page
		orderPane = new JPanel(new GridLayout(1, 1));
		// menuScrollPane = new JScrollPane(orderPane);
		// menuScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

		// table display area
		stoolPane = new JPanel(new FlowLayout());
		stoolPane.setVisible(true);

		// Put the table display pane in a scroll pane incase its too big
		tableScrollPane = new JScrollPane(stoolPane);
		tableScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

		// full horizontal split pane window
		leftSplitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, tableScrollPane, orderPane /* menuScrollPane */);
		leftSplitPane.setOneTouchExpandable(true);
		leftSplitPane.setDividerLocation(200);// (int) (screenSizeX * .80));

		// reservation area
		detailPane = new JPanel(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();

		// setup for layout
		newGuest = new JButton("New Guest");
		newGuest.setEnabled(false);
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridwidth = 2;
		c.ipady = 80;
		c.weightx = 1;
		c.gridx = 0;
		c.gridy = 1;
		detailPane.add(newGuest, c);

		lblDetail = new JLabel("Details", JLabel.CENTER);
		lblDetail.setFont(font);
		c.fill = GridBagConstraints.HORIZONTAL;
		c.ipady = 0;
		c.weightx = 1;
		c.gridx = 0;
		c.gridy = 2;
		detailPane.add(lblDetail, c);

		// show the active receipt
		// Put the table display pane in a scroll pane incase items are too big
		JScrollPane tableScrollPane = new JScrollPane(txtAreaReceipt);// jtbReceipt);
		
		c.fill = GridBagConstraints.HORIZONTAL;
		c.ipady = (int) (panelHeight * .40);
		c.weightx = 1;
		c.gridx = 0;
		c.gridy = 3;
		// c.weighty = 50;
		detailPane.add(tableScrollPane, c);

		// label for total bill
		lblTotal = new JLabel("TOTAL", JLabel.CENTER);
		lblTotal.setFont(font);
		c.fill = GridBagConstraints.HORIZONTAL;
		c.ipady = 0;
		c.weightx = 1;
		c.gridx = 0;
		c.gridy = 4;
		detailPane.add(lblTotal, c);

		// field showing total bill
		fldReceiptTot = new JTextField();
		fldReceiptTot.setFont(font);
		c.fill = GridBagConstraints.HORIZONTAL;
		c.ipady = 80;
		c.weightx = 1;
		c.gridx = 0;
		c.gridy = 5;
		detailPane.add(fldReceiptTot, c);

		// transfer ticket button
		transferTicket = new JButton("Transfer Ticket");
		transferTicket.setEnabled(false);// TODO if time to implement transfer
											// of ticket
		c.fill = GridBagConstraints.HORIZONTAL;
		c.ipady = 80;
		c.weightx = 1;
		c.gridx = 0;
		c.gridy = 6;
		c.insets = new Insets(5, 5, 5, 5); // top padding
		detailPane.add(transferTicket, c);

		closeTicket = new JButton("Close Ticket");
		c.fill = GridBagConstraints.HORIZONTAL;
		c.ipady = 80;
		c.weightx = 1;
		c.gridx = 0;
		c.gridy = 7;
		c.insets = new Insets(5, 5, 5, 5); // top padding
		// c.anchor = GridBagConstraints.PAGE_END;
		detailPane.add(closeTicket, c);

		// full split pane window
		splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftSplitPane, detailPane);// table
																							// display
																							// left,
																							// reservation
																							// window
																							// right
		splitPane.setOneTouchExpandable(true);
		splitPane.setDividerLocation((int) (panelWidth * .80));

		// add to hostess frame
		add(splitPane);
	}

	@Override
	public void tableChanged(TableModelEvent arg0) {
		// TODO
		int row = arg0.getFirstRow();
		int column = arg0.getColumn();
		TableModel model = (TableModel) arg0.getSource();
		String columnName = model.getColumnName(column);
		Object data = model.getValueAt(row, column);

		// TODO Do something with the data...
	}
}

// TODO lots here to do..
class MyTableModelRecipt extends AbstractTableModel {

	// TODO use vector becuase its synced..

	ArrayList<Receipt> rowData = new ArrayList<Receipt>(11);

	private String[] columnNames = { "Item Quant", "Item Name", "Item Price" };

	public int getColumnCount() {
		return columnNames.length;
	}

	public int getRowCount() {
		return rowData.size();
	}

	public String getColumnName(int col) {
		return columnNames[col];
	}

	public Object getValueAt(int row, int col) {
		Receipt temp = rowData.get(row);
		return temp.getColumn(col);
	}

	/*
	 * @SuppressWarnings("unchecked") public Class getColumnClass(int c) {
	 * return getValueAt(0, c).getClass(); }
	 */

	public void setValueAt(Object value, int row, int col) {
		Receipt temp = rowData.get(row);
		int quant = temp.getQuant(); // col 0
		String name = temp.getName(); // col 1
		double price = temp.getPrice(); // col 2

		switch (col) {
		case 0: {
			rowData.set(row, new Receipt((Integer) value, name, price));
			break;
		}
		case 1: {
			rowData.set(row, new Receipt(quant, (String) value, price));
			break;
		}
		case 2: {
			rowData.set(row, new Receipt(quant, name, (Double) value));
		}
		}

		fireTableCellUpdated(row, col);
	}
}